package com.zuqiuzhandui.wxapi;

import android.widget.Toast;

import com.code19.library.GsonUtil;
import com.code19.library.L;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zuqiuzhandui.R;
import com.zuqiuzhandui.liveing.base.BaseActivity;
import com.zuqiuzhandui.liveing.model.WXResultModel.GetAccessTokenWXResM;
import com.zuqiuzhandui.liveing.model.WXResultModel.UserInfoWXResM;
import com.zuqiuzhandui.liveing.net.ProgressSubscriber;
import com.zuqiuzhandui.liveing.net.SubscriberOnNextListener;
import com.zuqiuzhandui.liveing.net.WXMethods;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by yq on 2016-10-10.
 */

public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;


    @Override
    public void setViewId() {
        setContentView(R.layout.activity_wxentrty);
    }

    @Override
    public void initView() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, WEIXIN_APP_ID, false);
        api.handleIntent(getIntent(), this);

    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        int result = 0;
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = R.string.errcode_success;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = R.string.errcode_cancel;
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = R.string.errcode_deny;
                break;
            default:
                result = R.string.errcode_unknown;
                break;
        }

        Toast.makeText(this, result, Toast.LENGTH_LONG).show();
        L.d("微信返回的错误码是" + baseResp.errCode);
        getUserInfo(baseResp);


    }

    private void getUserInfo(BaseResp baseResp) {
        SendAuth.Resp resp = (SendAuth.Resp) baseResp;
        L.d(GsonUtil.objectToJson(resp));

        WXMethods.getInstance().wxService.getAccessToken(WEIXIN_APP_ID,WEIXIN_APP_SECRET,resp.code,GRANT_TYPE)
                .flatMap(new Func1<GetAccessTokenWXResM, Observable<UserInfoWXResM>>() {
                    @Override
                    public Observable<UserInfoWXResM> call(GetAccessTokenWXResM getAccessTokenWXResM) {
                        return WXMethods.getInstance().wxService.getUserInfo(getAccessTokenWXResM.getAccess_token(),getAccessTokenWXResM.getOpenid());
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<UserInfoWXResM>(new SubscriberOnNextListener<UserInfoWXResM>() {
                    @Override
                    public void onNext(UserInfoWXResM o) {
                        L.d("获取到的用户信息是"+GsonUtil.objectToJson(o));
                    }
                },this));

    }


}
